<template>
  <el-card>
    <!-- ! 搜索表单区域 -->
    <el-form inline size="small">
      <el-form-item label="角色名">
        <el-input
          v-model="queryData.name"
          placeholder="请输入角色名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="btnSearch">
          搜索
        </el-button>
        <el-button size="small" @click="btnReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- ! 操作区域 -->
    <el-row type="flex" justify="space-between">
      <div>
        <el-button
          size="small"
          type="primary"
          @click="$refs.addRole.showDialog = true"
        >
          新增
        </el-button>
      </div>
      <div>右侧内容</div>
    </el-row>
    <!-- ! 表格区域 -->
    <el-table :data="list" border>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="id" width="320px" label="角色ID"></el-table-column>
      <el-table-column prop="name" label="角色名称"></el-table-column>
      <el-table-column prop="description" label="角色描述"></el-table-column>
      <el-table-column prop="create_date" label="创建时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            circle
            icon="el-icon-edit"
            @click="editRow(scope.row.id)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            circle
            icon="el-icon-delete"
            @click="delRow(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- ! 分页区域 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page.sync="queryData.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryData.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
    ></el-pagination>
    <!-- ! 新增修改区域 -->
    <addRole ref="addRole" @updateList="initData" />
  </el-card>
</template>
<script>
import { getRoleListApi } from '@/api/role'
import addRole from './components/addRole.vue'
export default {
  components: { addRole },
  name: 'Role',
  data() {
    return {
      list: [], // 表格数据
      queryData: {
        // 列表的查询参数  【搜索表单、页码、每页数量】
        name: '',
        page: 1,
        limit: 10
      },
      total: 0 // 总数
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取表格数据
    async initData() {
      const { count, data } = await getRoleListApi(this.queryData)
      this.total = count
      this.list = data
    },
    // 每页数量切换事件
    sizeChange(val) {
      this.queryData.limit = val
      this.initData()
    },
    // 页码切换事件
    pageChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 搜索提交
    btnSearch() {
      this.queryData.page = 1
      this.initData()
    },
    // 搜索重置
    btnReset() {
      this.queryData = {
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 删除事件
    async delRow(id) {
      try {
        await this.confirm('确认删除吗？')
      } catch (error) {
        return console.log(error)
      }
      // await delUserApi(id)
      this.message.success('删除成功')
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    // 修改事件
    editRow(id) {
      this.$refs.addRole.getRowInfo(id)
    }
  }
}
</script>
<style scoped></style>
